<?php
namespace app\index\model;
use think\Model;
use think\Session;

class User extends Model 
{
    /**
     * 登录 成功返回1 失败返回0
     * @param $data(数组)
     */
	public function login ($data)
	{
//		dump($data);die;
        $info = $this->field(["id","name","image","level"])
            ->where($data)->find();
        if(empty($info)){
            //如果$info为空 ,则邮箱或密码有误
            return 0;
        }else{
            //如果$info不为空
            $info = $info->data;
            $home['name'] = $info['name'];
            $home['email'] = $data['email'];
            $home['image'] = $info['image'];
            $home['level'] = $info['level'];
            $home['id'] = $info['id'];
            //把用户的信息放入到session中
            Session::set("home",$home);
            return 1;
        }
	}

    /**
     * 注册 邮箱重复返回 2  成功返回 1   失败返回 0
     * @param $data
     */
    public function reg ($data)
    {
        //判断邮箱是否已经被注册
        $where['email'] = $data['email'];
        $info = $this->field(["id"])->where($where)->find();
        if(!empty($info)){
            return 2;
        }
        // 把信息写入数据库
        $id = $this->save($data);
        if($id > 0){
            //注册成功后,把昵称,邮箱放入到session
            $this->where("id=$this->id")->setInc("gold",10);
            $home['email'] = $data['email'];
            $home['name'] = $data['name'];
            $home['id'] = $this->id;
            Session::set("home",$home);
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 查询一条用户记录,根据$data 数组条件
     * @param $data
     * @return array|false|object|\PDOStatement|string|Model
     */
    public function selectUser ($data)
    {
        $info = $this->where($data)->find();
        if(empty($info)){
            return $info;
        }else{
            return $info->data;
        }
    }

    /**0
     * 根据$where条件数组,跟新一条用户数据
     * @param $where
     * @param $data
     * @return $this
     */
    public function updateUser ($where,$data)
    {
        return $this->where($where)->update($data);
    }

    /**
     * 用户某字段 $field 自减 $num
     * @param $field
     * @param $num
     */

    public function setDecUser ($where,$field,$num)
    {
        $this->where($where)->setDec($field,$num);
    }

    /**
     * 用户某字段 $field 自增$num
     * @param $field
     * @param $num
     */
    public function setIncUser ($where,$field,$num)
    {
        $this->where($where)->setInc($field,$num);
    }

    public function getUserPowerUrl ($id)
    {
        $info = $this->alias("u")
            ->join("fly_user_role ur","u.id=ur.user_id")
            ->join("fly_role r","r.id=ur.role_id")
            ->join("fly_role_power rp","rp.role_id=r.id")
            ->join("fly_power p","p.id=rp.power_id")
            ->field(["u.name","r.role_name","p.power_name","p.url"])
            ->where("u.id=$id")
            ->select();
        if(empty($info)){
            return $info;
        }else{
            foreach($info as $key => $val){
                $data[$key] = $val->data;
            }
            return $data;
        }
    }
}

?>